<?php
namespace App\Http\Controllers\Business;

use App\Models\WaterMeter;
use App\Models\WaterMeterRadix;
use App\Models\WaterMeterDayConsumption;
use Maatwebsite\Excel\Facades\Excel;
use Log;

class DataModule {
    //父节点列表
    public static function fatherNodeList() {
        $data = WaterMeter::where('is_father', 1)->select('id', 'name', 'number', 'enterprise_name', 'node_level')->get();
        return ['status'=>'success', 'data'=>$data];
    }
    //通过编号获取水表信息
    public static function getDetailByWaterMeterNumber($number) {
        if(!$number) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $data = WaterMeter::where('number', $number)
            ->select('id', 'name', 'enterprise_name', 'number', 'area', 'radix', 'updated_at', 'last_time', 'last_radix', 'latitude', 'longitude')
            ->first();
        return ['status'=>'success', 'data'=>$data];
    }
    //通过父节点获取子节点信息
    public static function balanceAnalyze($fatherNode, $date) {
        if(!$fatherNode || !$date) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $startTime = $date . " 00:00:00";
        $lastTime = $date . " 23:59:59";
        $fatherConsumption = WaterMeterDayConsumption::where('number', $fatherNode)->where('current_date', '>=', $startTime)
            ->where('current_date', '<=', $lastTime)->pluck('consumption')->first();
        $fatherConsumption = ($fatherConsumption == '')? 0: $fatherConsumption;
        $lists = WaterMeter::where('father_node', $fatherNode)
            ->select('id', 'name', 'enterprise_name', 'number', 'area', 'radix', 'updated_at', 'last_time', 'last_radix', 'latitude', 'longitude')
            ->orderBy('id', 'asc')->get();
        $childTotalConsumption = 0;
        $validCount = 0;
        foreach ($lists as $v => $k) {
            $number = $lists[$v]['number'];
            $consumption = WaterMeterDayConsumption::where('number', $number)->where('current_date', '>=', $startTime)
                ->where('current_date', '<=', $lastTime)->pluck('consumption')->first();
            if($consumption == '') {
                $lists[$v]['use_status'] = 0;
                $lists[$v]['consumption'] = 0;
            } else {
                $lists[$v]['use_status'] = 1;
                $lists[$v]['consumption'] = $consumption;
                $childTotalConsumption += $consumption;
                $validCount ++;
            }
        }
        if($fatherConsumption == 0) {
            $lossRate = '--';
        } else {
            $lossRate = round(($fatherConsumption-$childTotalConsumption) * 100 / $fatherConsumption, 2 );
        }

        $data = [
            'father_consumption' => $fatherConsumption,
            'child_lists' => $lists,
            'child_total_consumption' => $childTotalConsumption,
            'loss_rate' => $lossRate,
            'count' =>  count($lists),
            'valid_count' => $validCount,
            'invalid_count' => count($lists) - $validCount
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //分析不平衡的原因
    public static function notBalanceReason($fatherNode, $date) {
        if(!$fatherNode || !$date) {
            return ['status' => 'failed', 'message' => '参数错误'];
        }
        $startTime = $date . " 00:00:00";
        $lastTime = $date . " 23:59:59";
        $lastWeekStartTime = date('Y-m-d H:i:s', strtotime($startTime)- 3600*24*7);
        $lastWeekLastTime = date('Y-m-d H:i:s', strtotime($lastTime)- 3600*24*7);
        $twoWeeksAgoStartTime = date('Y-m-d H:i:s', strtotime($startTime)- 3600*24*14);
        $twoWeeksAgoLastTime = date('Y-m-d H:i:s', strtotime($lastTime)- 3600*24*14);
        $fatherConsumption = WaterMeterDayConsumption::where('number', $fatherNode)->where('current_date', '>=', $startTime)
            ->where('current_date', '<=', $lastTime)->pluck('consumption')->first();
        $fatherConsumption = ($fatherConsumption == '') ? 0 : $fatherConsumption;
        $fatherAvg = WaterMeterDayConsumption::where('number', $fatherNode)->avg('consumption');
        $fatherAvg = ($fatherAvg == '')?0: $fatherAvg;
        $fatherMin = WaterMeterDayConsumption::where('number', $fatherNode)->min('consumption');
        $fatherMin = ($fatherMin =='')?0:$fatherMin;
        $fatherLastWeekConsumption =   WaterMeterDayConsumption::where('number', $fatherNode)->where('current_date', '>=', $lastWeekStartTime)
            ->where('current_date', '<=', $lastWeekLastTime)->pluck('consumption')->first();
        $fatherLastWeekConsumption = ($fatherLastWeekConsumption == '') ? 0: $fatherLastWeekConsumption;
        $fatherTwoWeeksAgoConsumption =   WaterMeterDayConsumption::where('number', $fatherNode)->where('current_date', '>=', $twoWeeksAgoStartTime)
            ->where('current_date', '<=', $twoWeeksAgoLastTime)->pluck('consumption')->first();
        $fatherTwoWeeksAgoConsumption = ($fatherTwoWeeksAgoConsumption == '') ? 0: $fatherTwoWeeksAgoConsumption;
        if($fatherAvg != 0) {
            $fatherRate = round(($fatherConsumption - $fatherAvg) * 100 /$fatherAvg, 2);
        } else {
            $fatherRate = '--';
        }
        $lists = WaterMeter::where('father_node', $fatherNode)->select('id','number','name')->orderBy('id', 'asc')->get();
        foreach ($lists as $v => $k) {
            $number = $lists[$v]['number'];
            $consumption = WaterMeterDayConsumption::where('number', $number)->where('current_date', '>=', $startTime)
                ->where('current_date', '<=', $lastTime)->pluck('consumption')->first();
            $lastWeekConsumption = WaterMeterDayConsumption::where('number', $number)->where('current_date', '>=', $lastWeekStartTime)
                ->where('current_date', '<=', $lastWeekLastTime)->pluck('consumption')->first();
            $twoWeeksAgoConsumption = WaterMeterDayConsumption::where('number', $number)->where('current_date', '>=', $twoWeeksAgoStartTime)
                ->where('current_date', '<=', $twoWeeksAgoLastTime)->pluck('consumption')->first();
            $avg = WaterMeterDayConsumption::where('number', $number)->avg('consumption');
            $min = WaterMeterDayConsumption::where('number', $number)->min('consumption');
            $consumption = ($consumption == '') ? 0 : $consumption;
            $lastWeekConsumption = ($lastWeekConsumption == '') ? 0 : $lastWeekConsumption;
            $twoWeeksAgoConsumption = ($twoWeeksAgoConsumption == '') ? 0 : $twoWeeksAgoConsumption;
            $avg = ($avg == '') ? 0 : $avg;
            if($avg != 0) {
                $rate = round(($consumption - $avg) * 100 /$avg, 2);
            } else {
                $rate = '--';
            }
            $lists[$v]['consumption'] = $consumption;
            $lists[$v]['last_week_consumption'] = $lastWeekConsumption;
            $lists[$v]['two_weeks_ago_consumption'] = $twoWeeksAgoConsumption;
            $lists[$v]['avg'] = $avg;
            $lists[$v]['rate'] = $rate;
            $lists[$v]['min'] = ($min == '') ? 0 : $min;
        }
        $data = [
            'father_number' => $fatherNode,
            'father_consumption' => $fatherConsumption,
            'father_last_week_consumption' => $fatherLastWeekConsumption,
            'father_two_weeks_ago_consumption' => $fatherTwoWeeksAgoConsumption,
            'father_avg' => $fatherAvg,
            'father_rate' => $fatherRate,
            'father_min' => $fatherMin,
            'child_lists' => $lists
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //通过企业名称获取水表信息
    public static function waterMeterListByEnterpriseName($enterpriseName) {
        if(!$enterpriseName) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $lists = WaterMeter::where('enterprise_name', $enterpriseName)
            ->select('id', 'name', 'number')
            ->orderBy('number', 'asc')->get();
        return ['status'=>'success', 'data'=>$lists];
    }
    //比较两个水表信息
    public static function twoWaterMeterComparison($waterMeterId1, $waterMeterTime1, $waterMeterId2, $waterMeterTime2) {
        if(!$waterMeterId1 || !$waterMeterTime1 || !$waterMeterId2 || !$waterMeterTime2) {
            return ['status' => 'failed', 'message'=>'参数错误'];
        }
        if(($waterMeterId1 == $waterMeterId2) && ($waterMeterTime1 == $waterMeterTime2)) {
            return ['status' => 'failed', 'message'=>'请选择不一样的时间'];
        }
        $waterMeterNumber1 = WaterMeter::where('id', $waterMeterId1)->pluck('number')->first();
        $waterMeterNumber2 = WaterMeter::where('id', $waterMeterId2)->pluck('number')->first();
        $waterMeterName1 = WaterMeter::where('id', $waterMeterId1)->pluck('name')->first();
        $waterMeterName2 = WaterMeter::where('id', $waterMeterId2)->pluck('name')->first();
        $waterMeterEnterpriseName1 = WaterMeter::where('id', $waterMeterId1)->pluck('enterprise_name')->first();
        $waterMeterEnterpriseName2 = WaterMeter::where('id', $waterMeterId2)->pluck('enterprise_name')->first();
        $waterMeterRadixList1 = WaterMeterRadix::where('number', $waterMeterNumber1)
            ->where('created_at', '>=', $waterMeterTime1 . " 00:00:00")
            ->where('created_at', '<=', $waterMeterTime1 . " 23:59:59")
            ->get();
        $waterMeterRadixList2 =  WaterMeterRadix::where('number', $waterMeterNumber2)
            ->where('created_at', '>=', $waterMeterTime2 . " 00:00:00")
            ->where('created_at', '<=', $waterMeterTime2 . " 23:59:59")
            ->get();
        $data = [
            'name1' => $waterMeterName1,
            'name2' => $waterMeterName2,
            'number1' => $waterMeterNumber1,
            'number2' => $waterMeterNumber2,
            'enterprise_name1' => $waterMeterEnterpriseName1,
            'enterprise_name2' => $waterMeterEnterpriseName2,
            'radix_list1' => $waterMeterRadixList1,
            'radix_list2' => $waterMeterRadixList2
        ];
        return ['status'=>'success', 'data'=>$data];
    }
}

?>
